import { ApiProperty } from '@midwayjs/swagger';
import { getSchema, Rule, RuleType } from '@midwayjs/validate';
import { alternatives, StrIdNullValidator } from '..';
import { AreaSchema } from '../../schema';

export class SaveAreaValidator implements AreaSchema {
  @ApiProperty({
    type: 'string',
    title: '区域编码',
    example: 'null',
    description: '区域编码',
    required: true,
  })
  @Rule(RuleType.string().required())
  code: string;

  @ApiProperty({
    type: 'string',
    title: '区域名称',
    example: 'null',
    description: '区域名称',
    required: true,
  })
  @Rule(RuleType.string().required())
  name: string;

  @ApiProperty({
    type: 'number',
    title: '区域成绩',
    example: 'null',
    description: '区域成绩',
    required: true,
  })
  @Rule(RuleType.number().required())
  level: number;

  @ApiProperty({
    type: 'string',
    title: '父级区域编码',
    example: 'null',
    description: '父级区域编码',
    required: true,
  })
  @Rule(RuleType.string().required())
  pCode: string;

  @ApiProperty({
    type: StrIdNullValidator,
    title: '父级区域',
    example: 'null',
    description: '父级区域',
    required: false,
  })
  @Rule(getSchema(StrIdNullValidator).allow(null).allow({}))
  parent?: AreaSchema;
}

export class EditAreaValidator implements Partial<AreaSchema> {
  @ApiProperty({
    type: 'string',
    title: '区域编码',
    example: 'null',
    description: '区域编码',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  code?: string;

  @ApiProperty({
    type: 'string',
    title: '区域名称',
    example: 'null',
    description: '区域名称',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  name?: string;

  @ApiProperty({
    type: 'number',
    title: '区域成绩',
    example: 'null',
    description: '区域成绩',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  level?: number;

  @ApiProperty({
    type: 'string',
    title: '父级区域编码',
    example: 'null',
    description: '父级区域编码',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  pCode?: string;

  @ApiProperty({
    type: StrIdNullValidator,
    title: '父级区域',
    example: 'null',
    description: '父级区域',
    required: false,
  })
  @Rule(getSchema(StrIdNullValidator).allow(null).allow({}))
  parent?: AreaSchema;
}

export class AreaCodeValidator {
  @ApiProperty({
    type: 'string',
    title: '区域编码',
    example: 'null',
    description: '区域编码',
    required: true,
  })
  @Rule(RuleType.string().required())
  code: string;
}
